Page({
  data: {
    userInfo: null,
    rooturl: "https://www.zglianbang.com/api/miaozhimini/"
  },
  
  onLoad: function() {
    // 加载模拟数据
    this.setMockData();
  },
  
  // 设置模拟数据
  setMockData: function() {
    var that=this;
	var token = wx.getStorageSync('token');
	wx.request({
		url: that.data.rooturl + 'weixin/getUserInfo',
		data: {},
		header:{token:token},
		success: function (res) {
			wx.hideLoading();
			var data = res.data.data;
			console.log(data);
			that.setData({
				userInfo: data
			})
		}
	})
  },
  
  // 复制邀请码
  copyInviteCode: function() {
    wx.setClipboardData({
      data: this.data.userInfo.conCode,
      success: function() {
        wx.showToast({
          title: '邀请码已复制',
          icon: 'success'
        });
      }
    });
  },
  
  // 保存二维码到相册
  saveQrCode: function() {
    wx.showLoading({
      title: '保存中...',
    });
    
    // 下载二维码图片
    wx.downloadFile({
      url: this.data.userInfo.qcode,
      success: (res) => {
        if (res.statusCode === 200) {
          // 保存图片到相册
          wx.saveImageToPhotosAlbum({
            filePath: res.tempFilePath,
            success: () => {
              wx.hideLoading();
              wx.showToast({
                title: '保存成功',
                icon: 'success'
              });
            },
            fail: (err) => {
              wx.hideLoading();
              console.error('保存失败', err);
              
              // 如果是因为用户拒绝授权导致的失败
              if (err.errMsg.indexOf('auth deny') >= 0) {
                wx.showModal({
                  title: '提示',
                  content: '需要您授权保存图片到相册',
                  confirmText: '去授权',
                  success: (res) => {
                    if (res.confirm) {
                      wx.openSetting();
                    }
                  }
                });
              } else {
                wx.showToast({
                  title: '保存失败',
                  icon: 'none'
                });
              }
            }
          });
        } else {
          wx.hideLoading();
          wx.showToast({
            title: '图片下载失败',
            icon: 'none'
          });
        }
      },
      fail: () => {
        wx.hideLoading();
        wx.showToast({
          title: '图片下载失败',
          icon: 'none'
        });
      }
    });
  }
})